/**
 * @overview 创建热力图
 * @author wangxingxing
 * @description 鲸云CgsHeatMap
 * @update  2022.03.29
 */
class GHeatMap {
  constructor() {
    window.GHeatMapArray = []
  }

  //添加热力图
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const optionModified = {
      map: option.map || window.map.map3D,
      positions: option.positions || [],
      heatStyle: {
        gradient: option.gradient || {
          0.1: '#1bf321',
          0.2: '#5df31b',
          0.3: '#a5f31b',
          0.4: '#f3f31b',
          0.5: '#f3ae1b',
          0.6: '#f3761b',
          0.7: '#f3271b',
          0.8: '#f31b64',
          0.9: '#f31bb6',
          1.0: '#ca1bf3',
        },
        radius: option.radius || 15,
        blur: option.blur || 0.85,
      },
      flyTo: option.flyTo || false,
      show: option.show || true,
      style: {
        arc: option.arc || true,
        height: option.height || 200,
      },
    }
    const heatmaplayer = new CgsHeatMapLayer(optionModified)
    // heatmaplayer.feature3D.attribute = option.attribute;//支持添加自定义属性

    const heatmaplayerObject = {
      name: name,
      parentName: parentName,
      heatmaplayer: heatmaplayer,
    }
    window.GHeatMapArray.push(heatmaplayerObject)
  }

  //根据name移除单个热力图
  removeByName(name) {
    window.GHeatMapArray.forEach((item, index) => {
      if (item.name == name) {
        item.heatmaplayer.distory()
        window.GHeatMapArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批热力图
  removeByPatName(parentName) {
    const length = window.GHeatMapArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GHeatMapArray[i].parentName == parentName) {
        window.GHeatMapArray[i].heatmaplayer.distory()
        window.GHeatMapArray.splice(i, 1)
      }
    }
  }

  //移除所有热力图
  removeAll() {
    window.GHeatMapArray.forEach((item) => {
      item.heatmaplayer.distory()
    })
    window.GHeatMapArray = []
  }

  //根据name控制单个热力图的显隐
  setVisibleByName(name, boolean) {
    window.GHeatMapArray.forEach((item) => {
      if (item.name == name) {
        item.heatmaplayer.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批热力图的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GHeatMapArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.heatmaplayer.setVisible(boolean)
      }
    })
  }
}

export default GHeatMap
